chrome.runtime.onInstalled.addListener(() => {
  console.log("Extension installed!");
});

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  chrome.runtime.onMessage.addListener((message, sender) => {
    if (message.action === "close_current_tab" && sender.tab) {
      chrome.tabs.remove(sender.tab.id);
    }
  });

  if (request.action === "open_new_tab") {
    chrome.tabs.create({
      url: request.url,
      active: false, // 关键参数：不激活新标签页
    });
  }

  if (request.type === "status") {
    chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
      if (tabs.length > 0) {
        chrome.tabs.sendMessage(tabs[0].id, request, (response) => {
          sendResponse(response);
        });
      }
    });
    return true; // 表示异步响应
  }
});
